/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package cn.wildfire.chat.kit.conversation.pick;

import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.conversation.ChooseMoreConversationCallback;
import cn.wildfire.chat.kit.conversation.PickOreCreateConversationCallback;
import cn.wildfire.chat.kit.search.OnResultItemClickListener;
import cn.wildfire.chat.kit.search.SearchFragment;
import cn.wildfire.chat.kit.search.SearchableModule;
import cn.wildfire.chat.kit.search.module.ContactSearchModule;
import cn.wildfire.chat.kit.search.module.GroupSearchViewModule;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.GroupSearchResult;
import cn.wildfirechat.model.UserInfo;

import static butterknife.OnTextChanged.Callback.AFTER_TEXT_CHANGED;

abstract public class PickOrCreateConversationActivity extends WfcBaseActivity implements PickOreCreateConversationCallback {
    private SearchFragment searchFragment;
    private List<SearchableModule> searchableModules;
    private List<Conversation> chooseConversations;
    private boolean isMore;
    private boolean isSendOpen = false;
    @BindView(R2.id.searchEditText)
    EditText editText;
    @BindView(R2.id.toolbar_choose_more)
    Button toolbar_choose_more;
    private PickOrCreateConversationFragment pickOrCreateConversationFragment;

    @Override
    protected int contentLayout() {
        return R.layout.pick_or_create_conversation_activity;
    }
    @Override
    protected void afterViews() {
        pickOrCreateConversationFragment = new PickOrCreateConversationFragment();
        pickOrCreateConversationFragment.setListener(this);
        pickOrCreateConversationFragment.setChooseMoreListener(new ChooseMoreConversationCallback() {
            @Override
            public void onResultConversation(List<Conversation> conversations) {
                chooseConversations = conversations;
                if (conversations.size()>0) {
                    isSendOpen = true;
                    toolbar_choose_more.setText("发送");
                    toolbar_choose_more.setBackgroundColor(ContextCompat.getColor(PickOrCreateConversationActivity.this, R.color.green2));
                }else {
                    isSendOpen = false;
                    toolbar_choose_more.setText("单选");
                    toolbar_choose_more.setBackgroundColor(ContextCompat.getColor(PickOrCreateConversationActivity.this,R.color.transparent));
                }
            }
        });
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.containerFrameLayout, pickOrCreateConversationFragment)
                .commit();
        initSearch();
    }

    @OnClick(R2.id.toolbar_choose_more)
    public void onClick(View v) {
        if (v.getId()==R.id.toolbar_choose_more) {
            if (pickOrCreateConversationFragment!=null){
                if (isSendOpen){
                    isSendOpen = false;
                    onPickOrCreateMoreConversation(chooseConversations);
                    return;
                }
                if (isMore){
                    isMore = false;
                    toolbar_choose_more.setText("多选");
                }else {
                    isMore = true;
                    toolbar_choose_more.setText("单选");
                    toolbar_choose_more.setBackgroundColor(ContextCompat.getColor(this,R.color.transparent));
                }
                pickOrCreateConversationFragment.setChoose(isMore);
            }
        }
    }
    private void initSearch() {
        searchableModules = new ArrayList<>();
        SearchableModule module = new ContactSearchModule();
        module.setOnResultItemListener(new OnResultItemClickListener<UserInfo>() {
            @Override
            public void onResultItemClick(Fragment fragment, View itemView, View view, UserInfo userInfo) {
                Conversation conversation = new Conversation(Conversation.ConversationType.Single, userInfo.uid, 0);
                onPickOrCreateConversation(conversation);
            }
        });
        searchableModules.add(module);

        module = new GroupSearchViewModule();
        module.setOnResultItemListener(new OnResultItemClickListener<GroupSearchResult>() {
            @Override
            public void onResultItemClick(Fragment fragment, View itemView, View view, GroupSearchResult gr) {
                Conversation conversation = new Conversation(Conversation.ConversationType.Group, gr.groupInfo.target, 0);
                onPickOrCreateConversation(conversation);
            }
        });
        searchableModules.add(module);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        editText.setText("");
        editText.clearFocus();
    }

    @OnTextChanged(value = R2.id.searchEditText, callback = AFTER_TEXT_CHANGED)
    void search(Editable editable) {
        String keyword = editable.toString().trim();
        FragmentManager fragmentManager = getSupportFragmentManager();
        if (!TextUtils.isEmpty(keyword)) {
            if (fragmentManager.findFragmentByTag("search") == null) {
                searchFragment = new SearchFragment();
                fragmentManager.beginTransaction()
                        .add(R.id.containerFrameLayout, searchFragment, "search")
                        .addToBackStack("search-back")
                        .commit();
            }
            new Handler().post(() -> {
                searchFragment.search(keyword, searchableModules);
            });
        } else {
            getSupportFragmentManager().popBackStackImmediate();
        }
    }

}
